﻿// CanopyClusteringDlg.cpp: 实现文件
//

#include "pch.h"
#include "CanopyClusteringDlg.h"
#include "afxdialogex.h"


// CanopyClusteringDlg 对话框

CImageList Canimagelist;

IMPLEMENT_DYNAMIC(CanopyClusteringDlg, CDialogEx)

CanopyClusteringDlg::CanopyClusteringDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CANOPY, pParent)
	, m_checkbool(FALSE)
{

}

CanopyClusteringDlg::~CanopyClusteringDlg()
{
}

void CanopyClusteringDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_CLASS, m_ClusterList);
	DDX_Control(pDX, IDC_BUTTON_CLUSTERCOLOR, m_ClusterColor);
	DDX_Control(pDX, IDC_PROGRESS_CANOPY, m_CanoProgressCtrl);
	DDX_Control(pDX, IDC_CHECK_DISPLAY, m_Check);
	DDX_Check(pDX, IDC_CHECK_DISPLAY, m_checkbool);
}


BEGIN_MESSAGE_MAP(CanopyClusteringDlg, CDialogEx)

	ON_BN_CLICKED(IDC_BUTTON_CLUSTERING, &CanopyClusteringDlg::OnBnClickedButtonClustering)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_CLASS, &CanopyClusteringDlg::OnCustomdrawListClass)
	ON_BN_CLICKED(IDC_BUTTON_CLUSTERCHANGE, &CanopyClusteringDlg::OnBnClickedButtonClusterchange)
	ON_BN_CLICKED(IDC_BUTTON_CLUSTERCOLOR, &CanopyClusteringDlg::OnBnClickedButtonClustercolor)
	ON_BN_CLICKED(IDC_BUTTON_MERGECLUSTER, &CanopyClusteringDlg::OnBnClickedButtonMergecluster)
//	ON_NOTIFY(HDN_ITEMCHANGED, 0, &CanopyClusteringDlg::OnItemchangedListClass)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_CLASS, &CanopyClusteringDlg::OnItemchangedListClass)
END_MESSAGE_MAP()


// CanopyClusteringDlg 消息处理程序


void CanopyClusteringDlg::OnBnClickedButtonRuncanopy()
{
	CString strT1, strT2, strClassNum;
	GetDlgItemText(IDC_EDIT_CLASSNUM, strClassNum);
	m_pCanopyCluster->dT1 = _ttoi(strT1);
	m_pCanopyCluster->dT2 = _ttoi(strT2);
	int iClassNum = _ttoi(strClassNum);

	for (int ii = 0; ii < iClassNum; ii++)
	{
		m_pCanopyCluster->Clustering();
	}
	m_pCanopyCluster->ReClustering();
	m_pCanopyCluster->WriteIntoImage();
	m_pCanopyCluster->UpdataCluster();
	
	// TODO: 在此添加控件通知处理程序代码
}


BOOL CanopyClusteringDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	SetDlgItemText(IDC_EDITT1, _T("35"));
	SetDlgItemText(IDC_EDITT2, _T("25"));
	SetDlgItemText(IDC_EDIT_CLASSNUM, _T("20"));

	CRect rect;
	IsColorChange = 0;
	// 获取编程语言列表视图控件的位置和大小   
	m_ClusterList.GetClientRect(&rect);

	pProg = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS_CANOPY);
	pProg->SetRange(0, 100);//设置进度条范围
	pProg->SetPos(0);//设置进度条初始位置

	pProg->SetBarColor(RGB(1, 132, 127));//设置进度条前景色为马尔斯绿

	// 为列表视图控件添加全行选中和栅格风格   
	m_ClusterList.SetExtendedStyle(m_ClusterList.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	m_ClusterList.InsertColumn(0, _T("Color"), LVCFMT_CENTER, rect.Width() / 3, -1);
	m_ClusterList.InsertColumn(1, _T("No."), LVCFMT_CENTER, rect.Width() / 3, -1);
	m_ClusterList.InsertColumn(2, _T("Name"), LVCFMT_CENTER, rect.Width() / 3, -1);

	// TODO:  在此添加额外的初始化

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CanopyClusteringDlg::OnBnClickedButtonClustering()
{
	if (!m_pCanopyCluster->imgOriginal)
	{
		AfxMessageBox(_T("没有待分类图像"));
		return;
	}
	CString strT1, strT2, strClassNum;
	GetDlgItemText(IDC_EDITT1, strT1);
	GetDlgItemText(IDC_EDITT2, strT2);
	GetDlgItemText(IDC_EDIT_CLASSNUM, strClassNum);
	m_pCanopyCluster->dT1 = _ttoi(strT1);
	m_pCanopyCluster->dT2 = _ttoi(strT2);
	int iClassNum = _ttoi(strClassNum);
	m_pCanopyCluster->ReadOriData();
	for (int ii = 0; ii < iClassNum; ii++)
	{
		m_pCanopyCluster->Clustering();
		int pro = int((ii + 1) / float(iClassNum) * 100);

		pProg->SetPos(pro);//设置新的进度条进度
	}
	m_pCanopyCluster->ReClustering();
	m_pCanopyCluster->UpdataCluster();
	m_pCanopyCluster->WriteIntoImage();

	GetDlgItem(IDC_BUTTON_CLUSTERING)->EnableWindow(FALSE);
}


void CanopyClusteringDlg::OnCustomdrawListClass(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);

	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}


void CanopyClusteringDlg::OnBnClickedButtonClusterchange()
{
	if (!m_pCanopyCluster->imgOriginal)
	{
		AfxMessageBox(_T("没有待分类图像"));
		return;
	}

	CString strNum;
	CString strName;
	GetDlgItemText(IDC_EDIT_CLUSTERNUM, strNum);
	GetDlgItemText(IDC_EDIT_CLUSTERNEWNAME, strName);
	if (strNum == _T(""))
	{
		AfxMessageBox(_T("未选择需要修改的类编号"));
		return;
	}
	int iNum = _ttoi(strNum);
	CButton* pBtn = (CButton*)GetDlgItem(IDC_CHECK_DISPLAY);
	BOOL IsDisplay;
	int state = pBtn->GetCheck();
	if (state == 1) // 选中
	{
		IsDisplay = 1;
	}
	else // 取消选中
	{
		IsDisplay = 0;
	}
	if (IsColorChange == 0)
	{
		r = m_pCanopyCluster->m_ClassPoint[iNum - 1].R;
		g = m_pCanopyCluster->m_ClassPoint[iNum - 1].G;
		b = m_pCanopyCluster->m_ClassPoint[iNum - 1].B;
	}
	else
	{
		IsColorChange = 0;
	}

	m_pCanopyCluster->ChangeCluster(iNum, r, g, b, strName, IsDisplay);
	m_pCanopyCluster->WriteIntoImage();
	m_pCanopyCluster->UpdataCluster();
	// 将背景颜色修改
	// TODO: 在此添加控件通知处理程序代码
}


void CanopyClusteringDlg::OnBnClickedButtonClustercolor()
{
	COLORREF m_Color;  //从颜色对话框获取的颜色
	CColorDialog cd;
	if (cd.DoModal() == IDOK)
	{
		m_Color = cd.GetColor();
	}

	// 获得RGB数值
	r = BYTE(m_Color);
	g = BYTE(m_Color >> 8);
	b = BYTE(m_Color >> 16);
	IsColorChange = 1;
	// 修改按钮颜色
	m_ClusterColor.SetFaceColor(RGB(r, g, b), true);
}


void CanopyClusteringDlg::OnBnClickedButtonMergecluster()
{
	CString strName1;
	CString strName2;
	GetDlgItemText(IDC_EDIT_CLUSTER1, strName1);
	GetDlgItemText(IDC_EDIT_CLUSTER2, strName2);
	if (strName1 == _T("") || strName2 == _T(""))
	{
		AfxMessageBox(_T("未选择合并类或者被并类"));
		return;
	}
	int iNum1 = _ttoi(strName1);
	int iNum2 = _ttoi(strName2);

	m_pCanopyCluster->MergeCluster(iNum1, iNum2);
	m_pCanopyCluster->WriteIntoImage();
	m_pCanopyCluster->UpdataCluster();
	// TODO: 在此添加控件通知处理程序代码
}


BOOL CanopyClusteringDlg::DestroyWindow()
{
	m_pCanopyCluster->~CanopyClustering();

	return CDialogEx::DestroyWindow();
}


//void CanopyClusteringDlg::OnItemchangedListClass(NMHDR *pNMHDR, LRESULT *pResult)
//{
//	//LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
//	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
//
//	if (pNMLV->uNewState & LVIS_SELECTED)
//	{
//		iItemNum = pNMLV->iItem;
//
//		// select item pNMLV->iItem;
//		CString s;
//		s.Format(_T("Selected: %lu"), pNMLV->iItem);
//		MessageBox(s);
//	}
//	*pResult = 0;
//}


void CanopyClusteringDlg::OnItemchangedListClass(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	if (pNMLV->uNewState & LVIS_SELECTED)
	{
		iItemNum = pNMLV->iItem;

		// select item pNMLV->iItem;
		/*CString s;
		s.Format(_T("Selected: %lu"), pNMLV->iItem);
		MessageBox(s);*/
		CString strNum;
		strNum.Format(_T("%d"), m_pCanopyCluster->m_ClassPoint[iItemNum].iNum);
		SetDlgItemText(IDC_EDIT_CLUSTERNUM, strNum);
		SetDlgItemText(IDC_EDIT_CLUSTERNEWNAME, m_pCanopyCluster->m_ClassPoint[iItemNum].strName);
		int ir = m_pCanopyCluster->m_ClassPoint[iItemNum].R;
		int ig = m_pCanopyCluster->m_ClassPoint[iItemNum].G;
		int ib = m_pCanopyCluster->m_ClassPoint[iItemNum].B;
		m_ClusterColor.SetFaceColor(RGB(ir, ig, ib), true);
		if (m_pCanopyCluster->m_ClassPoint[iItemNum].IsDisplay == TRUE)
			((CButton*)GetDlgItem(IDC_CHECK_DISPLAY))->SetCheck(BST_CHECKED);
		else
			((CButton*)GetDlgItem(IDC_CHECK_DISPLAY))->SetCheck(BST_UNCHECKED);
	}
	*pResult = 0;
}
